<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/09/15 22:17:02 $)
// $RCSfile: cron_parser.php,v $ : $Revision: 1.15 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

$template->loadLanguageSection("emailparser");
$template->loadLanguageSection("staff_tickets");
$template->loadLanguageSection("tickets_auditlogs");
require_once ("./includes/functions_mime.php");
require_once ("./includes/functions_users.php");
require_once ("./modules/parser/functions_parsercore.php");
require_once ("./modules/parser/functions_parserrules.php");

if (!count($_SWIFT["queuecache"]))
{
	echo "No Queues Found!";
	exit;
}

if ($_GET["debug"] == "1")
{
	$_SWIFT["clidebug"] = true;
	$_SWIFT["dbgfilename"] = "crondbg_".buildHash();
}

define("INPARSER", "1");

$_SWIFT["startparser"] = fetchmicrotime();

if ($module->isRegistered(MODULE_TICKETS))
{
	require_once ("./modules/tickets/functions_ticketcore.php");
	require_once ("./modules/tickets/functions_ticketmain.php");
	require_once ("./modules/tickets/functions_alertrules.php");
	require_once ("./modules/tickets/functions_attachments.php");
	require_once ("./modules/tickets/functions_search.php");
	require_once ("./modules/tickets/functions_sla.php");
} else {
	echo "Ticket Module is not registered";
	exit;
}

if (!extension_loaded('imap'))
{
	echo "ERROR: Your PHP is not compiled with IMAP Support.<BR /> *NIX Users should recompile the PHP with the --with-imap flag and Windows users can simply uncomment the extension='php_imap.dll' line in their php.ini";
	exit;
}

$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."emailqueues` ORDER BY `emailqueueid` ASC;", 4);
while ($dbCore->nextRecord(4))
{
	if ($dbCore->Record4["fetchtype"] == "pipe")
	{
		continue;
	}
	if ($dbCore->Record4["fetchtype"] == "imapssl")
	{
		$fetchtype = "imap/ssl/novalidate-cert";
	} else if ($dbCore->Record4["fetchtype"] == "imap") {
		$fetchtype = "imap";
	} else if ($dbCore->Record4["fetchtype"] == "pop3ssl") {
		$fetchtype = "pop3/ssl/novalidate-cert";
	} else {
		$fetchtype = "pop3";
	}

	$_account = $dbCore->Record4;

	$mbox = imap_open("{". $dbCore->Record4["host"].":".$dbCore->Record4["port"] ."/". $fetchtype ."/notls}INBOX", $dbCore->Record4["username"], $dbCore->Record4["userpassword"]);
	if ($mbox) {
		$rejected = 0;
		$accepted = 0;
		$totalmsg = imap_num_msg($mbox);
		echo "Total Messages in Inbox: $totalmsg<br>";
		@set_time_limit(0);
		for ($i=1;$i<=$totalmsg; ++$i) {
			if ($_account["fetchtype"] == "imapssl" || $_account["fetchtype"] == "imap")
			{
//				$msgid = $i-1;
				$msgid = $i;
			} else {
				$msgid = $i;
			}

			if ($accepted > intval($_SWIFT["settings"]["pr_procno"]))
			{
				break;
			}
			echo "Fetching Msg Num: $msgid<BR />";
			unset($_uidtype);
			if ($_account["fetchtype"] == "pop3" || $_account["fetchtype"] == "pop3ssl")
			{
				echo "Account is POP3<BR />";
//				$_popheader = imap_headerinfo($mbox, $msgid);
				$_msguid = $i;
				$_uidtype = FT_UID;
			} else {
				echo "Account is IMAP<BR />";
				$_msguid = imap_uid($mbox, $msgid);
				$_uidtype = FT_UID;
			}
			echo "Fetching Msg UID: $_msguid<BR />";
			$structd = imap_fetchstructure($mbox, $_msguid, $_uidtype);
			$db = $structd->bytes;
			$kb = $db/1024;
			$mb = $kb/1024;
			if ($kb > $_SWIFT["settings"]["pr_sizelimit"]) {
				// Reject the Message
				$rejected++;
			} else {
				$accepted++;
				$headers = imap_fetchheader($mbox, $_msguid, $_uidtype);
				$message = imap_body($mbox, $_msguid, $_uidtype);
				$data = preg_replace("#(\r\n|\r|\n)#s", SWIFT_CRLF, $headers.$message); unset($headers); unset($message);

				if ($_account["fetchtype"] == "pop3" || $_account["fetchtype"] == "pop3ssl")
				{
					imap_delete($mbox, $msgid);
				} else {
					imap_delete($mbox, $_msguid, $_uidtype);
				}
				parseRawMimeData($data);
			}
		}
		imap_expunge($mbox);
		imap_close($mbox);
	} else {
		var_dump(imap_errors());
	}
}

echo "$totalmsg Total Messages<br>Rejected Messages: $rejected<br>Accepted Messages: $accepted";

global $cron;
$cron->logentry($_SWIFT["cronid"], "success");
?>